//
//  MianTabBar.swift
//  ZSYWeiBo
//
//  Created by 周饰宇 on 2017/4/13.
//  Copyright © 2017年 zhouShiYu. All rights reserved.
//

import UIKit

class MianTabBar: UITabBar {
    
    private let buttonCount = 5
    
    // layoutSubviews中间不要写太耗时的代码
    // 当系统布局方法不能满足需求，在此方法中调整布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        print(#function)
        
        print("==================")
        
        print(composeButton)
        
        let w = bounds.width / CGFloat(buttonCount)
        let h = bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        
        // UITabBarButton 是私有接口，在开发中不要使用
        // 变量所有子视图
        var index = 0
        for v in subviews {
            
            
            // 判断控件
            if v is UIControl && !(v is UIButton){
                
                v.frame = rect.offsetBy(dx: CGFloat(index) * w, dy: 0)
                
                index += index == 1 ? 2 : 1
            }
        }
        
        composeButton.frame = rect.offsetBy(dx: 2 * w, dy: 0)
    }
    
    
    lazy var composeButton : UIButton = {
        
        let btn = UIButton()
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), for: UIControlState.normal)
         btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
         btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: UIControlState.normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        // 闭包，提前准备好的代码，在需要的时候执行
        self.addSubview(btn)
        
        return btn
    }()
   
    
    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
